#!/usr/bin/env perl -w
# Time-stamp: <12 Jul 02 12:34:04 giron1> 

=head1 NAME

optext.perl - optional extension encoder - decoder

=head1 SYNOPSIS

 optext.perl [-help] string | keywords list

 examples:
 used as decoder: optext.perl cond.e130.m1
 returns: estima ge array fine+music hiway

 used as encoder: optext.perl estima ge array fine+music hiway
 returns: .e130.m1

=head1 DESCRIPTION

This programs decodes an extension or encodes a list of keywords in an
extension.  

=head1 BUGS

For encoding B<all> keywords must be given in the right order. If not
the returned extension will be wrong.

=head1 SEE ALSO

For more precision about the procedures provided for encoding see also
L<OPT_EXT_HTML/OPT_EXT>

=head1 AUTHOR

 $Id: optext.perl,v 1.1 2007/10/08 09:26:05 schweitz Exp $
 Franck Giron

=cut

#----------------------------------------------------------------------
# Packages 
#----------------------------------------------------------------------
#use strict;
use OPTIONS;
use OPT_EXT;

#----------------------------------------------------------------------
# MAIN
#----------------------------------------------------------------------
# Define the set of options

my $OptList = new OPTIONS;

$OptList->condsnr('Do' => 0, 'Usage' => 'read condition files sorted by SNR');
$OptList->debug('Do' => 0, 'Usage' => 'debuging mode');
$OptList->pkg('string' => 'DCL_Car_Noise',
	      'Usage' => "<name>\t: package name used for conditions");

# Command processing
$OptList->GetOpt || die "Check your options\n";

# Check the arguments
my $numargs = @ARGV;

# Eventually define which package is used for the conversion if the argument 
# string contains a string like eX where X is an number
if ($numargs == 1 and $ARGV[$numargs-1] =~ m/e(\d+)/) {
  $OptList->{'pkg'}{'string'} = OPT_EXT::GetPackage($1);
  if (not defined $OptList->{'pkg'}{'string'}) {
    warn "environment e$1 not yet defined, check your package definitions.\n";
    exit 0;
  }
}

my $pkg = new OPT_EXT $OptList->{'pkg'}{'string'};

my @Noise_Cond = @{$pkg->{'data'}->{'Conditions_Keys'}};
my $Noise_Cond_Numb = $#Noise_Cond + 1;

# Display condition file by SNR
if ($OptList->{'condsnr'}{'Do'}) {
  $pkg->ReadNoiseInfos;
  foreach (sort keys %{$pkg->{'data'}->{'infos'}}) {
    print "$pkg->{'data'}->{'infos'}{$_}{'SNR'} : $_\n";
  }
  exit 0;
}

my $usage = ( ($numargs != $Noise_Cond_Numb) && ($numargs != 1) );

if ($usage) {
  $OptList->Usage("string | @Noise_Cond");
}

SWITCH: {
  if ($numargs == 1) {
    # Take care of string given in a directory form
    if ($ARGV[0] =~ m/\//) {
      $_ = $ARGV[0];
      s/^\.?\///;s/\/$//;
      my @envlist = split '/',$_;
      my %Cond = ( 'env' => \@envlist, 'mic' => undef, 'sf' => undef);
      my $ext = $pkg->Cond2Ext(\%Cond);
      print "$ext\n";
      last SWITCH;      
    } else {
      my %Cond = $pkg->Ext2Cond($ARGV[0]);
	  foreach (sort keys %Cond) {
		if (defined $Cond{$_}) {
		  # do we have an array
		  local *sym = $Cond{$_};
		  if (@sym) {
			print "$_ = @sym\n";
		  }
		  else {
			print "$_ = $Cond{$_}\n";
		  }
		}
		else {
		  print "$_ = undef\n";
		}
	  }
      last SWITCH;
    }
  }
  if ($numargs == $Noise_Cond_Numb) {
    my %Cond = ('env' => \@ARGV, 'mic' => undef, 'sf' => undef);
    my $ext = $pkg->Cond2Ext(\%Cond);
    print "$ext\n";
    last SWITCH;
  } 
}

exit;

__END__


=head1 COMMANDS

The following commands are defined:

=over 10

=item -debug

debuging mode

=back

=head1 OPTIONS

The following options are defined:

=over 14

=item -pkg I<name>

package name used for conditions (default: DCL_Car_Noise)

=back

